package com.lqm.service.home.impl;

import com.lqm.dao.HomeUserDao;
import com.lqm.entity.home.HomeUser;
import com.lqm.service.home.HomeUserService;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.transaction.Transactional;
import java.util.List;

/**
 * @program: CrowdFunding
 * @description
 * @author: lqm
 * @create: 2018-12-07 12:34
 */
@Service
@Transactional
public class HomeUserServiceImpl implements HomeUserService {

    @Autowired
    HomeUserDao homeUserDao;


    @Override
    public List<HomeUser> findAll(int page,int limit) {
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        Pageable pageable = PageRequest.of(page - 1, limit, sort);
        Page<HomeUser> all = homeUserDao.findAll(pageable);
        return all.getContent();
    }
    @Override
    public long getCount(){
        return homeUserDao.count();
    }

    @Override
    public HomeUser save(HomeUser homeUser){
        return homeUserDao.save(homeUser);
    }

    @Override
    public void setHomeUserStatus(HomeUser homeUser,int status) {
        homeUserDao.setHomeUserStatus(homeUser,status);
    }

    @Override
    public HomeUser login(HomeUser homeUser) {

        String psd = DigestUtils.md5DigestAsHex(homeUser.getPassword().getBytes());
        homeUser.setPassword(psd);
        HomeUser user = homeUserDao.findByUsernameAndPassword(homeUser.getUsername(),homeUser.getPassword());

        return user;
    }

    @Override
    public boolean changePassword(HomeUser user,String oldpassword, String password, String rePassword) {
        System.out.println(user);
        if (!password.equals("")&&!rePassword.equals("")&&!"".equals(oldpassword)){
            String oldMd5p = DigestUtils.md5DigestAsHex(oldpassword.getBytes());
            HomeUser homeUser = homeUserDao.getById(user.getId());
            if (oldMd5p.equals(homeUser.getPassword())){
                String reMd5p = DigestUtils.md5DigestAsHex(rePassword.getBytes());
                homeUser.setPassword(reMd5p);
                return true;
            }
        }




        return false;


/*
            if (password.equals(rePassword)){
                System.out.println(homeUser);
//                HomeUser user = homeUserDao.getById(homeUser.getId());
//                System.out.println(user);
//                System.out.println("user.getPassword()"+user.getPassword());
                String md5Psd = DigestUtils.md5DigestAsHex(password.getBytes());
                System.out.println("md5Psd"+md5Psd);
              if (user.getPassword().equals(md5Psd)){
                    Integer integer = homeUserDao.changePassword(user, md5Psd);
                    if (integer!=null&&integer>0){
                        return true;
                    }else{
                        return false;
                    }

                }*/
    }

    @Override
    public HomeUser sign(HomeUser homeUser) {
        String psd = DigestUtils.md5DigestAsHex(homeUser.getPassword().getBytes());
        homeUser.setPassword(psd);

        HomeUser userByUsername = homeUserDao.getHomeUserByUsername(homeUser.getUsername());
        //用户名不存在，可以注册
        if (userByUsername==null){
            return homeUserDao.save(homeUser);
        }
        return null;
    }

    @Override
    public HomeUser getOneById(Integer id) {
        return homeUserDao.getById(id);
    }
}
